package com.yumeng.common.helper;


import com.yumeng.common.data.constants.DateConstants;
import com.yumeng.common.exception.YumengException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wxd
 * @date 2021/12/2 15:19
 */
public class DateHelper {



    public static Date nowDate(){
        return new Date();
    }

    public static String nowDateStr(){
        return nowDateTimeStr(DateConstants.YYYY_MM_DD);
    }

    public static String nowDateTimeStr(){
        return nowDateTimeStr(DateConstants.YYYY_MM_DD_HH_MM_SS);
    }

    public static final String nowDateTimeStr(final String format) {
        return dateToStr(format, new Date());
    }

    public static final String dateTimeToStr(final Date date) {
        return new SimpleDateFormat(DateConstants.YYYY_MM_DD_HH_MM_SS).format(date);
    }

    public static final String dateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date strToDate(final String format, final String ts) {
        if (StringUtils.isBlank(ts)){
            return null;
        }
        try {
            return DateUtils.parseDate(ts, format);
        }catch (ParseException e) {
            throw new YumengException(e);
        }
    }

    public static final Date strToDate(final String ts) {
        if (StringUtils.isBlank(ts)){
            return null;
        }
        try {
            return DateUtils.parseDate(ts, DateConstants.parseDatePatterns);
        }catch (ParseException e) {
            throw new YumengException(e);
        }
    }

    public static final Date strToDateTime(final String ts) {
        if (StringUtils.isBlank(ts)){
            return null;
        }
        try {
            return DateUtils.parseDate(ts, DateConstants.parseDateTimePatterns);
        }catch (ParseException e) {
            throw new YumengException(e);
        }
    }

    /**
     * 将日期值扩展为时间值（开始）
     * @param startTime
     * @return
     */
    public static String setStartTime(String startTime){
        if (StringUtils.isNotBlank(startTime)) {
            try {
                DateUtils.parseDate(startTime, DateConstants.parseDefaultPatterns);
                startTime = startTime.substring(0, 10) + " 00:00:00";
            } catch (ParseException e) {
                throw new YumengException("日期格式异常：%s", startTime);
            }
        }
        return startTime;
    }

    /**
     * 将日期值扩展为时间值（结束）
     * @param endTime
     * @return
     */
    public static String setEndTime(String endTime){
        if (StringUtils.isNotBlank(endTime)) {
            try {
                DateUtils.parseDate(endTime, DateConstants.parseDefaultPatterns);
                endTime = endTime.substring(0, 10) + " 23:59:59";
            } catch (ParseException e) {
                throw new YumengException("日期格式异常：%s", endTime);
            }
        }
        return endTime;
    }

}
